﻿
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.ViewModels.Configuration.DataGrid;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigConsumablesCtrlViewModel : BaseViewModel<ConfigConsumablesCtrlViewModel>
    {
        #region Variables
        private string _seachValue = "";
        private ObservableCollection<ConfigConsumablesModel> _configConsumablesModel = new ObservableCollection<ConfigConsumablesModel>();

        private ObservableCollection<ConfigConsumablesModel> _displayConfigConsumablesModel = new ObservableCollection<ConfigConsumablesModel>();
        private ConfigConsumablesModel _selectedP;
        #endregion

        #region Constructor

        public ConfigConsumablesCtrlViewModel()
        {
            InitConfigConsumablesModel();
        }

        #endregion

        #region Properties
        public ObservableCollection<ConfigConsumablesModel> ConfigConsumablesModel
        {
            get
            {
                return _configConsumablesModel;
            }

            set
            {
                _configConsumablesModel = value;
                RaisePropertyChanged("ConfigConsumablesModel");
            }
        }


        public ObservableCollection<ConfigConsumablesModel> DisplayConfigConsumablesModel
        {
            get
            {
                _displayConfigConsumablesModel.Clear();
                List<ConfigConsumablesModel> lst = ConfigConsumablesModel.ToList().Where(O =>O.Name.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _displayConfigConsumablesModel.Add(item);
                }
                return _displayConfigConsumablesModel;
            }

            set
            {
                ConfigConsumablesModel = value;
                RaisePropertyChanged("DisplayConfigConsumablesModel");
            }
        }

        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigConsumablesModel");
            }
        }

        public ConfigConsumablesModel SelectedP
        {
            get
            {
                return _selectedP;
            }

            set
            {
                if (value ==null) return;
                //SelectedSechduledP = value;
                _selectedP = value;
            }
        }
        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigConsumablesModelService.Instance.Update(sender as ConfigConsumablesModel);
        }
        private void AddConfigConsumablesModel(ConfigConsumablesModel obj)
        {
            List<ConfigConsumablesModel> list = DataManager.Instance.ConfigConsumablesModelList.ToList();
            //MedicineConfigCtrlViewModel.Instance.InitialDrugList();
            //SingleDrugConfigCtrlViewModel.Instance.InitialDrugList();
            //AnesSystem.ViewModels.Operation.CQ.CQTL.MedicineConfigCtrlViewModel.Instance.InitialDrugList();
            ConfigConsumablesModelService.Instance.Update(obj);

            //if (obj.Id != new ObjectId("000000000000000000000000"))
            //{

            //    ConfigConsumablesModelService.Instance.Update(obj);
            //    ConfigConsumablesModel.Add(obj);
            //}
            //else ConfigConsumablesModelService.Instance.Insert(obj);
        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigConsumablesModel(ConfigConsumablesModel configConsumablesModel)
        {
            if (configConsumablesModel != null)
            {
                ConfigConsumablesModel.Remove(configConsumablesModel);
                RaisePropertyChanged("DisplayConfigConsumablesModel");
                ConfigConsumablesModelService.Instance.DeleteById(configConsumablesModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigConsumablesModel model = new ConfigConsumablesModel();
            model.Code = "consum_" +DateTime.Now.Ticks;
            model.Name = "请编辑器材名";
            DisplayConfigConsumablesModel.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
            ConfigConsumablesModelService.Instance.AddUpdate(model);
            DataManager.Instance.ConfigConsumablesModelList.Add(model);
        }
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigConsumablesModel(sender as ConfigConsumablesModel);
        }
        private void InitConfigConsumablesModel()
        {
            IEnumerable<ConfigConsumablesModel> list = ConfigConsumablesModelService.Instance.FindAll();
            foreach (var item in list)
            {
                _configConsumablesModel.Add(item);

            }
        }
        #endregion

        #region EventHandlers

        
        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion

    }
}
